import net from 'net';
import { logger } from '../../../models/LogModel';
import EthTokenExtract from '../../coin/ethToken/EthTokenExtract';

var status = false;
var queue = [];

const server = net.createServer((socket) => {
  socket.on('data', data => {
    const str = Buffer.from(data).toString('utf8');
    queue.push(str);
    if(!status) {
      status = true;
      start();
    }
  })
  socket.end('end');
}).on('error', (error) => {
  console.log(error);
})

server.listen(9090, () => {
  logger('ethExtract').info('ethToken提币服务开启', server.address());
})

async function start() {
  const data = JSON.parse(queue.shift());
  logger('ethExtract').info('开始处理', data, '剩余数量', queue.length);
  // 提币流程
  const ethTokenExtract = new EthTokenExtract(data.address, data.amount, data.contract_address, data.orderId);
  await ethTokenExtract.send().catch(error => {
    console.log(error);
  });
  await sellp(2000);
  if(queue.length > 0){
    await start();
  }else{
    logger('ethExtract').info('执行结束');
    status = false;
  }
}

function sellp(time) {
  return new Promise((relove, reject) => {
    setTimeout(() => {
      relove();
    }, time);
  })
}
